package com.comm;

import com.dao.IMylog;
import com.model.MyLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Order
@Component
@Aspect
public class LogAopAspect {
    @Autowired
    private IMylog iMylog;

    @Around("@annotation(com.comm.LogAnno)")
    public Object auoundAd(ProceedingJoinPoint pjp){
        System.out.println("进入环形通知方法");
        //获取方法签名
        MethodSignature methodSignature= (MethodSignature) pjp.getSignature();
        //获取方法
        Method method=methodSignature.getMethod();
        //获取方法上面的注解
        LogAnno logAnno=method.getAnnotation(LogAnno.class);
        String oper=logAnno.OperateType();
        //获取访问接口时的各种操作
        MyLog sysLog=new MyLog();
        sysLog.setUserid(04);
        sysLog.setOpname("接口描述："+oper);
        sysLog.setLog(HttpContextUtil.getIPAddress());


        Object result=null;
        try{
            long begin = System.currentTimeMillis();
            try {
                result = pjp.proceed();
            } catch (Throwable e){
                e.printStackTrace();
            }

            sysLog.setOpname("正常");
        } catch (Exception e){
            sysLog.setOpname("失败");
        }finally {
            iMylog.addMyLog(sysLog);
            System.out.println("存储成功");
        }
        System.out.println("结束环形通知方法");
        return result;
    }
}
